﻿@using System
@using System.Linq
@using Microsoft.AspNetCore.Components
@using MudBlazor
@using MudBlazor.Docs.Models
@using Toolbelt.Blazor.HeadElement
@using MudBlazor.Docs.Extensions
@using System.Collections.Generic
@using System.Text.RegularExpressions

@*html head (for server prerendering)*@
<Title>@(GetTitle())</Title>
<Meta Name="keywords" Content="@GetKeywords()" />
<Meta Property="og:title" Content="@GetTitle()" />
<Meta Name="twitter:title" Content="@GetTitle()" />
@if (!string.IsNullOrEmpty(SubTitle))
{
    <Meta Name="description" Content="@(SubTitle)" />
    <Meta Property="og:description" Content="@SubTitle" />
    <Meta Name="twitter:description" Content="@SubTitle" />
}
@if (IsApiPage && !string.IsNullOrEmpty(ComponentLink))
{
    
}

<div class="docs-page-header mb-12">
    <MudText Typo="@Typo.h3">@Title</MudText>
    <MudText>@GetSubTitle() @Description</MudText>
</div>
<div id="mudads">
    @if (RenderAds)
    { 
        <MudElement HtmlTag="script" async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CESDLK3E&placement=mudblazorcom" id="_carbonads_js"></MudElement>
    }
</div>
@if (!IsApiPage && GetComponentTypeName() != null)
{
    <DocsPageSection api-link-section>
        <SectionHeader Title="API" />
        <div class="docs-page-apilinks">
            <ul class="my-2">
                <li>
                    <MudLink Href="@GetApiLink()">
                        <CodeInline>@($"<{GetComponentTypeName()}>")</CodeInline>
                    </MudLink>
                </li>
                @if(GetChildComponents() != null)
                {
                    @foreach(var child in GetChildComponents())
                    {
                        <li>
                            <MudLink Href="@($"api/{child.Name.Replace("`1", "").ToLower().Replace("mud","")}")" Class="ms-1">
                                - <CodeInline>@($"<{child.Name.Replace("`1", "")}>")</CodeInline>
                            </MudLink>
                        </li>
                    }
                }
            </ul>
        </div>
    </DocsPageSection>
}
@if (IsApiPage && !string.IsNullOrEmpty(ComponentLink))
{
    <Link Rel="canonical" Href="@($"https://mudblazor.com/{ComponentPageLink}")" />
    <DocsPageSection api-link-section>
        <SectionHeader Title="Pages" />
        <div class="docs-page-apilinks">
            <ul class="my-2">
                <li>
                    <MudLink Href="@ComponentPageLink">
                        <CodeInline>@($"{GetParentComponentPage()}")</CodeInline>
                    </MudLink>
                </li>
            </ul>
        </div>
    </DocsPageSection>
}

@code {

    public bool RenderAds { get; set; }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            RenderAds = true;
            StateHasChanged();
        }
    }

    [Inject] public NavigationManager NavigationManager { get; set; }
    [Inject] IMenuService MenuService { get; set; }

    [Parameter] public string Title { get; set; }
    [Parameter] public string SubTitle { get; set; }
    [Parameter] public string Keywords { get; set; }
    [Parameter] public RenderFragment Description { get; set; }
    [Parameter] public string ComponentLink { get; set; }

    private string GetApiLink() => $"api/{GetComponentName()}";
    private string ComponentPage;
    private string ComponentPageLink;

    private string GetComponentName()
    {
        return NavigationManager.Uri.ToString().Split('/', StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
    }

    private bool IsApiPage
    {
        get
        {
            return NavigationManager.Uri.ToString().Contains("/api/");
        }
    }

    private string GetComponentTypeName() => ApiLink.GetTypeFromComponentLink(GetComponentName())?.Name.Replace("`1", "");

    private List<Type> GetChildComponents()
    {
        return MenuService.Components.Where(c => c.Component == ApiLink.GetTypeFromComponentLink(GetComponentName()) && c.ChildComponents != null).SelectMany(l => l.ChildComponents).ToList();
    }

    private string GetParentComponentPage()
    {
        var currentComp = ApiLink.GetTypeFromComponentLink(GetComponentName());
        var parent = MenuService.GetParent(currentComp);
        var test = parent;
        if (parent.Component != null)
        {
            ComponentPage = parent.Name;
            ComponentPageLink = $"components/{parent.Link}";
        }
        else
        {
            var groupComp = parent.GroupItems.Elements.FirstOrDefault(x => x.Component == currentComp);
            ComponentPage = groupComp.Name;
            ComponentPageLink = $"components/{groupComp.Link}";
        }

        return ComponentPage;
    }

    string GetTitle() => $"{Title} - MudBlazor";

    string GetSubTitle()
    {
        if (string.IsNullOrEmpty(SubTitle))
            return "";
        return SubTitle.TrimEnd('.') + ".";
    }

    string GetKeywords()
    {
        var keywords = new HashSet<string>(Regex.Split(Keywords??"", @",\s"));
        keywords.Add(Title);
        keywords.Add(GetComponentTypeName());
        keywords.Add(GetComponentTypeName()?.Replace("Mud", ""));
        keywords.Add("mudblazor");
        keywords.Add("blazor");
        keywords.Add("component");
        keywords.Add("material design");
        keywords.Remove("");
        keywords.Remove(null);
        return string.Join(", ", keywords);
    }
}